﻿using Microsoft.SharePoint;

namespace BoldBrick.SharePoint.CommonControls.Extensions
{
	/// <summary>
	/// Extension class for <see cref="SPContentTypeCollection"/> objects
	/// </summary>
	public static class SPContentTypeCollectionEx
	{
		/// <summary>
		/// Gets <see cref="SPContentType"/> object if this content type is child content type of specified <see cref="SPContentTypeId"/> id
		/// </summary>
		/// <param name="collection">Collection of content types</param>
		/// <param name="id">ID of parent content type</param>
		/// <returns>Returns <see cref="SPContentType"/> object if this content type is child content type of specified <see cref="SPContentTypeId"/> id, otherwise returns null</returns>
		public static SPContentType GetChildContentType(this SPContentTypeCollection collection, SPContentTypeId id)
		{
			SPContentTypeId bestMatchContentTypeID = collection.BestMatch(id);
			if (!bestMatchContentTypeID.IsChildOf(id))
				return null;

			SPContentType contentType = collection[bestMatchContentTypeID];
			return contentType;
		}
	}
}
